package com.woniu.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.entity.FlowerInfo;
import com.woniu.mapper.FlowerInfoMapper;
import com.woniu.service.FlowerInfoService;
import com.woniu.vo.SortProductVo;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Service
public class FlowerInfoServiceImpl implements FlowerInfoService {

    @Resource
    FlowerInfoMapper flowerInfoMapper;

    @Override
//    @Cacheable(value = "flowerInfo",key = "#root.methodName+#root.args[0]+#root.args[1]")
    public PageInfo<FlowerInfo> findAllFlowerInfo(int pageNum, int pageSize) {
        Page<FlowerInfo> page = PageHelper.startPage(pageNum, pageSize);
        flowerInfoMapper.selectAll();
        return page.toPageInfo();    // toPageInfo：将 page 对象转换为 PageInfo<FlowerInfo> 对象并返回
    }

    @Override
    public FlowerInfo findFlowerInfoByFlowerInfoName(String flowerInfoName) {
        Example example = new Example(FlowerInfo.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("flowerInfoName", flowerInfoName);
        return flowerInfoMapper.selectOneByExample(example);
    }

    @Override
    @CacheEvict(value = "flowerInfo", allEntries = true)
    // 清除名称为flowerInfo的缓存，因为新增之后，要读取数据库，不能读取缓存中的角色，所以要清空缓存
    public int addFlowerInfo(FlowerInfo flowerInfo) {
        return flowerInfoMapper.insertSelective(flowerInfo);
    }

    @Override
    public List<FlowerInfo> selectFlowerInfoNameTo(String flowerInfoName) {
        Example example = new Example(FlowerInfo.class);
        Example.Criteria criteria = example.createCriteria();
        if (flowerInfoName != null && flowerInfoName.length() > 0) {
            criteria.andLike("flowerName", "%" + flowerInfoName + "%");
        }
        return flowerInfoMapper.selectByExample(example);
    }

    @Override
    public int editFlowerInfo(FlowerInfo flowerInfo) {
        FlowerInfo flowerInfoByFlowerInfoName = this.findFlowerInfoByFlowerInfoName(flowerInfo.getFlowerName());
        if (flowerInfoByFlowerInfoName == null) {
            return flowerInfoMapper.updateByPrimaryKeySelective(flowerInfo);
        } else {
            return 0;
        }
    }

    @Override
    public int deleteFlowerInfo(int id) {
        return flowerInfoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<FlowerInfo> sortFlowerInfoBySearchNameAndSortOrder(SortProductVo sortProductVo) {
        if (sortProductVo.getSearchName() != null && sortProductVo.getSearchName().length() > 0) {
            return flowerInfoMapper.sortFlowerInfoBySearchNameAndSortOrder(sortProductVo.getSearchName(),
                    sortProductVo.getSortProperty(),
                    sortProductVo.getSortOrder()
//                    sortProductVo.getPageNum(),
//                    sortProductVo.getPageSize()
            );
        }
        return null;
    }

    @Override
    public List<FlowerInfo> selectFlowerInfoNameToPage(SortProductVo sortProductVo) {
        PageHelper.startPage(Integer.parseInt(sortProductVo.getPageNum()), Integer.parseInt(sortProductVo.getPageSize()));

        Example example = new Example(FlowerInfo.class);
        Example.Criteria criteria = example.createCriteria();
        if (sortProductVo.getSearchName()!= null && sortProductVo.getSearchName().length() > 0) {
            criteria.andLike("flowerName", "%" + sortProductVo.getSearchName() + "%");
        }
        return flowerInfoMapper.selectByExample(example);
    }

}

